package com.whately.core.redis.utils;

import com.whately.core.base.model.RedisKeyBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisCacheHashUtils {

    private final Logger logger = LoggerFactory.getLogger(RedisCacheHashUtils.class);

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisTemplateUtils redisTemplateUtils;

    /**************************************************************************
     *     hash
     *************************************************************************/
    // 首次添加hash的key，必须设置过期时间
    public void putAllAndExpire(RedisKeyBuilder key, Map<? extends String, ? extends String> m, long timeout, TimeUnit timeUnit) {
        redisTemplate.opsForHash().putAll(key.getKey(), m);
        redisTemplateUtils.expire(key,timeout,timeUnit);
    }

    public void putAndKeepExistExpireTime(RedisKeyBuilder key, String hashKey, String value, long timeout, TimeUnit timeUnit) {
        //则新增put
        redisTemplate.opsForHash().put(key.getKey(), hashKey, value);

        //存在过期时间，则不再重新设置，否则设置
        if(-1 == redisTemplateUtils.ttl(key)){
            redisTemplateUtils.expire(key,timeout,timeUnit);
        }
    }

   /* public void put(RedisKeyBuilder key, String hashKey, String value) {
        redisTemplate.opsForHash().put(key.getKey(), hashKey, value);
    }*/

   /* public Boolean putIfAbsent(RedisKeyBuilder key, String hashKey, String value) {
        return redisTemplate.opsForHash().putIfAbsent(key.getKey(), hashKey, value);
    }*/

    public Long incrementAndExpire(RedisKeyBuilder key, String hashKey, long delta, long timeout, TimeUnit timeUnit) {
        long l= redisTemplate.opsForHash().increment(key.getKey(), hashKey, delta);
        redisTemplateUtils.expire(key,timeout,timeUnit);
        return l;

    }

    public Double incrementAndExpire(RedisKeyBuilder key, String hashKey, double delta, long timeout, TimeUnit timeUnit) {
        Double d= redisTemplate.opsForHash().increment(key.getKey(), hashKey, delta);
        redisTemplateUtils.expire(key,timeout,timeUnit);
        return d;
    }

    /*public List<Object> values(RedisKeyBuilder key) {
        return redisTemplate.opsForHash().values(key.getKey());
    }*/

    public void delete(RedisKeyBuilder key, Object... hashKeys) {
        redisTemplate.opsForHash().delete(key.getKey(), hashKeys);
    }

    public Boolean hasKey(RedisKeyBuilder key, Object hashKey) {
        return redisTemplate.opsForHash().hasKey(key.getKey(), hashKey);

    }

    public String get(RedisKeyBuilder key, Object hashKey) {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        return hashOperations.get(key.getKey(), hashKey);

    }

    public List<String> multiGet(RedisKeyBuilder key, Collection<String> hashKeys) {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        return hashOperations.multiGet(key.getKey(), hashKeys);

    }


    public Set<String> keys(RedisKeyBuilder key) {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        return hashOperations.keys(key.getKey());
    }

    public Long size(RedisKeyBuilder key) {
        return redisTemplate.opsForHash().size(key.getKey());
    }



    public Map<String, String> entries(RedisKeyBuilder key) {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        return hashOperations.entries(key.getKey());
    }

}
